#include "GSM.h"

GPIO_TypeDef* GSM_PORT[GSMnPIN] = {	GSM_PWRKEY_PORT,
									GSM_STATUS_PORT,
								 	GSM_RI_PORT,
								 	GSM_DTR_PORT};

const uint16_t GSM_PIN[GSMnPIN] = { 	GSM_PWRKEY_PIN,
									GSM_STATUS_PIN,	
									GSM_RI_PIN,
									GSM_DTR_PIN};

const uint32_t GSM_CLK[GSMnPIN] = { 	GSM_PWRKEY_CLK,
									GSM_STATUS_CLK,
									GSM_RI_CLK,
									GSM_DTR_CLK};

// UART Receiver buffer
#define GSM_RX_BUFFER_SIZE 128
volatile unsigned char gsm_rx_buffer[GSM_RX_BUFFER_SIZE];
volatile unsigned int gsm_rx_wr_index,gsm_rx_rd_index,gsm_rx_counter;

// This flag is set on USART Receiver buffer overflow
volatile unsigned char gsm_rx_buffer_overflow;

void GSMInitController(){
	GPIO_InitTypeDef  GPIO_InitStructure;

  /* Enable the GPIO_OUTPUT Clock */ 
	RCC_APB2PeriphClockCmd(GSM_PWRKEY_CLK, ENABLE);
	RCC_APB2PeriphClockCmd(GSM_STATUS_CLK, ENABLE);
	RCC_APB2PeriphClockCmd(GSM_RI_CLK, ENABLE);	
	RCC_APB2PeriphClockCmd(GSM_DTR_CLK, ENABLE);


	/* Configure the GSM_PWRKEY_PIN */
	GPIO_InitStructure.GPIO_Pin = GSM_PWRKEY_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  	GPIO_Init(GSM_PORT[GSM_PWRKEY], &GPIO_InitStructure);

	/* Configure the GSM_RI_PIN */
	GPIO_InitStructure.GPIO_Pin = GSM_STATUS_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  	GPIO_Init(GSM_PORT[GSM_STATUS], &GPIO_InitStructure);

	/* Configure the GSM_RI_PIN */
	GPIO_InitStructure.GPIO_Pin = GSM_RI_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  	GPIO_Init(GSM_PORT[GSM_RI], &GPIO_InitStructure);
	
	/* Configure the GSM_DTR_PIN */
	GPIO_InitStructure.GPIO_Pin = GSM_DTR_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  	GPIO_Init(GSM_PORT[GSM_DTR], &GPIO_InitStructure);

}

void GSM_PWRKEY_On(){
	GSM_PORT[GSM_PWRKEY]->BRR = GSM_PIN[GSM_PWRKEY]; 
}

void GSM_PWRKEY_Off(){
	GSM_PORT[GSM_PWRKEY]->BSRR = GSM_PIN[GSM_PWRKEY]; 
}

uint32_t GSM_GetStatus(){
	return GPIO_ReadInputDataBit(GSM_PORT[GSM_STATUS], GSM_PIN[GSM_STATUS]);
}

uint32_t GSM_GetRI(){
	return GPIO_ReadInputDataBit(GSM_PORT[GSM_RI], GSM_PIN[GSM_RI]);
}



void UART_GSM_ISR(void){
	unsigned char data;
	if(USART_GetITStatus(COM_GSM, USART_IT_RXNE) != RESET)
	{
		data = USART_ReceiveData(COM_GSM);
		UART_DEBUG_SendByte(data);
		gsm_rx_buffer[gsm_rx_wr_index] = data;
		if (++gsm_rx_wr_index == GSM_RX_BUFFER_SIZE) gsm_rx_wr_index = 0;
		if (++gsm_rx_counter == GSM_RX_BUFFER_SIZE)
		{
			gsm_rx_counter = 0;
			gsm_rx_buffer_overflow = 1;
		}
	}
}

void UART_GSM_SendByte(unsigned char data)
{
	USART_SendData(COM_GSM, data);
	while(USART_GetFlagStatus(COM_GSM, USART_FLAG_TXE) == RESET);
}

void UART_GSM_SendString(unsigned char *data)
{
	while(*data != NULL){
		UART_GSM_SendByte(*data);
		data++;
	}
}

uint8_t UART_GSM_ReceivedByte(void)
{
	uint8_t data;
	while (gsm_rx_counter == 0);
	data = gsm_rx_buffer[gsm_rx_rd_index];
	if (++gsm_rx_rd_index == GSM_RX_BUFFER_SIZE) gsm_rx_rd_index=0;
	--gsm_rx_counter;
	return data;
}


void UART_GSM_ReceivedString(uint8_t *data_buff, uint8_t data_len)
{
	uint8_t i;
	for(i=0;i<data_len;i++){
		data_buff[i] = UART_GSM_ReceivedByte();
	}
}

uint8_t UART_GSM_RXAvailable(void)
{
	return gsm_rx_counter;
}

void UART_GSM_RXFlush(void)
{
	while(UART_GSM_RXAvailable()){
		UART_GSM_ReceivedByte();			//dummy read
	}
}

